#include <iostream>

using std::cout;
using std::endl;

class Point
{
public:
    Point(int ix = 0, int iy = 0)
    : _ix(ix)
    , _iy(iy)
    {
        cout << "Point(int,int)" << endl;
    }

    //如果不显式提供的情况下，系统也会自动提供一个拷贝构造函数
    Point(Point & rhs)
    : _ix(rhs._ix)
    , _iy(rhs._iy)
    {
        cout << "Point(const Point&)" << endl;
    }

    ~Point() {
        cout << "~Point()" << endl;
    }

    void print() 
    {
        cout << "(" << _ix << "," << _iy << ")" << endl;
    }

private:
    int _ix;
    int _iy;
};

//形参是对象
void func1(Point pt)
{
    cout << "func1()" << endl;
    cout << "pt:";
    pt.print();
}

void test1()
{
    Point pt1(10, 11);
    func1(pt1);//形参初始化: Point pt = pt1;
}

//函数的返回值是对象
Point func2() 
{
    cout << "func2()" << endl;
    Point pt1(20, 21);
    cout << "pt1:";
    pt1.print();
    return pt1;//执行return语句时，会进行复制
}

void test2()
{
    //1 是一个字面值常量,无法取地址, 是一个右值
    //&1; 
    int & ref = 1;//error  非const引用无法绑定到右值
    Point & rhs = Point(10, 11);
    const int & ref2 = 1;//const引用可以绑定到右值

    //其生命周期很短，只在这一句存在
    //&func2();//error, 无法取地址，是一个临时对象   
    Point p = func2();

    //&Point(10, 11);//临时对象，右值
    Point p2 = Point(10, 11);

    int num = 1;
    &num;//可以取地址，称为左值
}


int test0()
{
    int a = 1;
    int b = a;
    int c(a);//C++支持

    //对象的复制
    Point pt1(10, 11);
    // const Point & rhs = pt1
    //Point pt2 = pt1;//调用一个拷贝构造函数
    Point pt2(pt1);//与上一句等价

    cout << "pt1:";
    pt1.print();

    cout << "pt2:";
    pt2.print();

    return 0;
}

int main()
{
    /* test1(); */
    test2();
    return 0;
}

